
$.fn.serializeObject = function () {
    "use strict";

    var result = {};
    var extend = function (i, element) {
        var node = result[element.name];

        // If node with same name exists already, need to convert it to an array as it
        // is a multi-value field (i.e., checkboxes)

        if ('undefined' !== typeof node && node !== null) {
            if ($.isArray(node)) {
                node.push(element.value);
            } else {
                result[element.name] = [node, element.value];
            }
        } else {
            result[element.name] = element.value;
        }
    };

    $.each(this.serializeArray(), extend);
    return result;
};

jQuery.fn.load2 = function (url, params, callback) {

    var selector, response, type,
        self = this,
        off = url.indexOf(" ");

    if (off >= 0) {
        selector = url.slice(off, url.length);
        url = url.slice(0, off);
    }

    // If it's a function
    if (jQuery.isFunction(params)) {

        // We assume that it's the callback
        callback = params;
        params = undefined;

        // Otherwise, build a param string
    } else if (params && typeof params === "object") {
        type = "GET";
    }

    // If we have elements to modify, make the request
    if (self.length > 0) {
        jQuery.ajax({
            url: url,

            // if "type" variable is undefined, then "GET" method will be used
            type: type,
            dataType: "html",
            data: params
        }).done(function (responseText) {

            // Save response for use in complete callback
            response = arguments;

            self.html(selector ?

                // If a selector was specified, locate the right elements in a dummy div
                // Exclude scripts to avoid IE 'Permission Denied' errors
                jQuery("<div>").append(jQuery.parseHTML(responseText)).find(selector) :

                // Otherwise use the full result
                responseText);

        }).complete(callback && function (jqXHR, status) {
            self.each(callback, response || [jqXHR.responseText, status, jqXHR]);
        });
    }

    return this;
};

/**
 * 扩充String原型,字符串模板格式化
 * <code>
 *     Array数据: '{0}'.format('hello world') ;
 *     json数据:'{name}'.format({'name':'hello world'});
 *     输出结果:hello world
 * </code>
 * @author Gongle
 * @date 2013年4月22日10:50:29
 */
if (!String.prototype.format) {
    String.prototype.format = function () {
        var args = arguments;
        if (Object.prototype.toString.call(args[0]) == '[object Object]') {
            var _jsonData = args[0];
            return this.replace(/{([^{}]+)}/gm, function (match, name) {
                return typeof _jsonData[name] != 'undefined'
                    ? _jsonData[name]
                    : match;
            });
        }
        return this.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != 'undefined'
                ? args[number]
                : match;
        });
    };
}
/***
 * Array添加indexOf方法.
 */
if (!Array.prototype.indexOf) {
    Array.prototype.indexOf = function (searchElement, fromIndex) {
        var i,
            pivot = (fromIndex) ? fromIndex : 0,
            length;

        if (!this) {
            throw new TypeError();
        }

        length = this.length;

        if (length === 0 || pivot >= length) {
            return -1;
        }

        if (pivot < 0) {
            pivot = length - Math.abs(pivot);
        }

        for (i = pivot; i < length; i++) {
            if (this[i] === searchElement) {
                return i;
            }
        }
        return -1;
    };
}
/**
 * concat 多个数组.
 * @param 数据
 */
Array.prototype.pushArray = function () {
    var toPush = this.concat.apply([], arguments);
    for (var i = 0, len = toPush.length; i < len; ++i) {
        this.push(toPush[i]);
    }
};

/*! http://mths.be/startswith v0.2.0 by @mathias */
if (!String.prototype.startsWith) {
    (function () {
        'use strict'; // needed to support `apply`/`call` with `undefined`/`null`
        var defineProperty = (function () {
            // IE 8 only supports `Object.defineProperty` on DOM elements
            try {
                var object = {};
                var $defineProperty = Object.defineProperty;
                var result = $defineProperty(object, object, object) && $defineProperty;
            } catch (error) {
            }
            return result;
        }());
        var toString = {}.toString;
        var startsWith = function (search) {
            if (this == null) {
                throw TypeError();
            }
            var string = String(this);
            if (search && toString.call(search) == '[object RegExp]') {
                throw TypeError();
            }
            var stringLength = string.length;
            var searchString = String(search);
            var searchLength = searchString.length;
            var position = arguments.length > 1 ? arguments[1] : undefined;
            // `ToInteger`
            var pos = position ? Number(position) : 0;
            if (pos != pos) { // better `isNaN`
                pos = 0;
            }
            var start = Math.min(Math.max(pos, 0), stringLength);
            // Avoid the `indexOf` call if no match is possible
            if (searchLength + start > stringLength) {
                return false;
            }
            var index = -1;
            while (++index < searchLength) {
                if (string.charCodeAt(start + index) != searchString.charCodeAt(index)) {
                    return false;
                }
            }
            return true;
        };
        if (defineProperty) {
            defineProperty(String.prototype, 'startsWith', {
                'value': startsWith,
                'configurable': true,
                'writable': true
            });
        } else {
            String.prototype.startsWith = startsWith;
        }
    }());
}
///================jquery 插件=================//
//设置select只读
$.fn.selectReadOnly = function () {
    return this.each(function () {
        var tem = $(this).children('option').index($("option:selected"));
        $(this).change(function () {
            $(this).children('option').eq(tem).attr("selected", true);
        });
    })
}
//获取DOM范围内的模板引擎.
$.fn.getTpl = function () {
    var obj = {},$this =$(this);
    this.each(function () {
        console.log($this.prop('name'))
        console.log($this.prop('id'))
        obj[$this.prop('name') || $this.prop('id')] = $this.html();
    })
    return obj;
}
//=================公用函数===============//


//===========javascript console log ============//
window.P= (function (P) {
    //===iframe下载=======//
    P.downloadURL = function downloadURL(url) {
        var hiddenIFrameID = '_hiddenDownloader',
            iframe = document.getElementById(hiddenIFrameID);
        if (iframe === null) {
            iframe = document.createElement('iframe');
            iframe.id = hiddenIFrameID;
            iframe.style.display = 'none';
            document.body.appendChild(iframe);
        }
        iframe.src = url;
    };
    P.getTpl = function (selector) {
        var obj = {};
        $(selector && (selector + ' ') + '[type="text/x-mustache"]').each(function () {
            var $this = $(this)
            obj[$this.prop('name') || $this.prop('id')] = $this.html();
        });
        return obj;
    }
    return P;
})(window.P || {});

$(function () {
    /**
     * 导航栏样式控制
     */
    $('#table-navbar').on('click', 'li', function (e) {
        e.preventDefault();
        $(this).addClass('active').siblings('.active').removeClass('active');
    });
});//Dom ready
